/*
copyright:    2009-2010 by Alexander D.
license:      GPL


This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/

﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using WAM.API;
using System.IO;

namespace WAM
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        TreeNode node;
        private void Form1_Load(object sender, EventArgs e)
        {
            WTF wtf = new WTF();
            Misc misc = new Misc();
            string[] Account = wtf.GetAccounts();
            for (short i = 0; i < Account.Length; ++i)
            {
                AddBox(boxAccs,Account[i]);
                AddBox(boxAccs2, Account[i]);
            }
        }

        public void AddBox(ListBox box, string txt)
        {
            box.Items.Add(txt);
        }

        private void cmdToRight_Click(object sender, EventArgs e)
        {
            Misc misc = new Misc();
            if (checkAccBindings.Checked == true)
            {
                File.Copy(misc.iPath + "WTF\\Account\\" + boxAccs.SelectedItem.ToString() + "\\bindings-cache.wtf", misc.iPath + "WTF\\Account\\" + boxAccs2.SelectedItem.ToString() + "\\bindings-cache.wtf", true);
            }
            if (checkAccMacros.Checked == true)
            {
                File.Copy(misc.iPath + "WTF\\Account\\" + boxAccs.SelectedItem.ToString() + "\\macros-cache.txt", misc.iPath + "WTF\\Account\\" + boxAccs2.SelectedItem.ToString() + "\\macros-cache.txt", true);
            }
            if (checkAccVariables.Checked == true)
            {
                lblStatus.Text = "Copying Variables";
                File.Copy(misc.iPath + "WTF\\Account\\" + boxAccs.SelectedItem.ToString() + "\\SavedVariables.lua", misc.iPath + "WTF\\Account\\" + boxAccs2.SelectedItem.ToString() + "\\SavedVariables.lua", true);
                string[] Variables = Directory.GetFiles(misc.iPath + "WTF\\Account\\" + boxAccs.SelectedItem.ToString() + "\\SavedVariables");
                progBar.Minimum = 0;
                progBar.Maximum = Variables.Length;
                progBar.Value = 0;
                for (int i = 0; i < Variables.Length; ++i)
                {
                    //AddBox(boxAccs2, Variables[i].Remove(0,(misc.iPath.Length+boxAccs.SelectedItem.ToString().Length+28)));
                    File.Copy(Variables[i], misc.iPath + 
                        "WTF\\Account\\" + boxAccs2.SelectedItem.ToString() + 
                        "\\SavedVariables\\"+Variables[i].Remove(0,(misc.iPath.Length+boxAccs.SelectedItem.ToString().Length+28)), true);
                    progBar.Value = i;
                }
            }
            lblStatus.Text = "Finished";
        }

        private void boxAccs_SelectedIndexChanged(object sender, EventArgs e)
        {
            treeChar.Nodes.Clear();
            WTF wtf = new WTF();
            string[] Realms = wtf.GetRealms(boxAccs.SelectedItem.ToString());
            for (short i = 0; i < Realms.Length; ++i)
            {
                if (Realms[i] != "SavedVariables")
                {
                    node = treeChar.Nodes.Add(Realms[i]);
                    string[] Chars = wtf.GetCharacter(boxAccs.SelectedItem.ToString(), Realms[i]);
                    for (short c = 0; c < Chars.Length; ++c)
                    {
                        node.Nodes.Add(Chars[c]);
                    }
                }
            }
        }

        private void boxAccs2_SelectedIndexChanged(object sender, EventArgs e)
        {
            treeChar2.Nodes.Clear();
            WTF wtf = new WTF();
            string[] Realms = wtf.GetRealms(boxAccs2.SelectedItem.ToString());
            for (short i = 0; i < Realms.Length; ++i)
            {
                if (Realms[i] != "SavedVariables")
                {
                    node = treeChar2.Nodes.Add(Realms[i]);
                    string[] Chars = wtf.GetCharacter(boxAccs2.SelectedItem.ToString(), Realms[i]);
                    for (short c = 0; c < Chars.Length; ++c)
                    {
                        node.Nodes.Add(Chars[c]);
                    }
                }
            }
        }
        private void CharToChar(string file)
        {
            Misc misc = new Misc();
            File.Copy(misc.iPath + "WTF\\Account\\" + 
                boxAccs.SelectedItem.ToString() + "\\" + 
                treeChar.SelectedNode.FullPath.Split('\\')[0] + "\\" + 
                treeChar.SelectedNode.Text + "\\" + file, misc.iPath + 
                "WTF\\Account\\" + boxAccs2.SelectedItem.ToString() + "\\" +
                treeChar2.SelectedNode.FullPath.Split('\\')[0] + "\\" + 
                treeChar2.SelectedNode.Text + "\\" + file, true);
        }
        private void cmdCharToChar_Click(object sender, EventArgs e)
        {
            Misc misc = new Misc();
            if (checkCharBindings.Checked == true)
            {
                CharToChar("bindings-cache.wtf");
            }
            if (checkCharMacros.Checked == true)
            {
                CharToChar("macros-cache.txt");
            }
            if (checkCharVariables.Checked == true)
            {
                CharToChar("SavedVariables.lua");
                string[] Variables = Directory.GetFiles(misc.iPath + "WTF\\Account\\" + boxAccs.SelectedItem.ToString() + "\\" + 
                treeChar.SelectedNode.FullPath.Split('\\')[0] + "\\" + treeChar.SelectedNode.Text + "\\SavedVariables");
                progBar.Minimum = 0;
                progBar.Maximum = Variables.Length;
                progBar.Value = 0;
                for (int i = 0; i < Variables.Length; ++i)
                {
                    File.Copy(Variables[i], misc.iPath + "WTF\\Account\\" + boxAccs2.SelectedItem.ToString() + "\\" + treeChar2.SelectedNode.FullPath.Split('\\')[0] + "\\" + treeChar2.SelectedNode.Text + "\\" + "SavedVariables" + "\\" + 
                        Variables[i].Remove(0, (misc.iPath.Length + boxAccs.SelectedItem.ToString().Length + treeChar.SelectedNode.FullPath.Split('\\')[0].Length + treeChar.SelectedNode.Text.Length + 30)), true);
                    progBar.Value = i;
                }
            }
            if (checkCharAddons.Checked == true)
            {
                CharToChar("AddOns.txt");
            }
            lblStatus.Text = "Finished";
        }
    }
}
